<template>
	<view>
		<view>
			<image mode="aspectFit" class="logo" src="../../static/pmi_logo.png"></image>
		</view>
		<form class="form">
			<view class="action padding-xl">
				<text class="text-left text-xxl">登录到</text>
				<text class="padding-left-sm text-left text-sm">pmi.org</text>
			</view>
			<view class="cu-form-group">
				<view class="title">账号</view>
				<input placeholder="Pmi账号" v-model="username" name="username"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">密码</view>
				<input placeholder="Pmi密码" v-model="password" password="true" type="text" name="password"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">保存账号到手机</view>
				<checkbox ref="myCheck" :class="save?checked:''" :checked="save"></checkbox>
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-purple lg" @tap="loginHandler">登录</button>
			</view>
		</form>

		<view @tap="about" class="flex flex-direction padding-bottom-sm">
			<text class="text-center text-blue">《隐私说明》</text>
		</view>
		<view class="text-xxl solid-top text-center ">
			<text class="text-grey text-center text-sm">已有 {{num}} 位考生查询了成绩</text>
		</view>
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-purple justify-end">
					<view class="content">隐私说明</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl text-left">
					<p>{{content}}</p>
					<p >测试账号:<text class="text-red">demo</text>，密码:<text class="text-red">demo</text></p>					
				</view>
				<view class="padding flex flex-direction">
					<button class="cu-btn bg-purple lg" @tap="hideModal">知道了</button>
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='error'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-purple justify-end">
					<view class="content">出错了</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl text-left">
					{{error}}
				</view>
				<view class="padding flex flex-direction">
					<button class="cu-btn bg-purple lg" @tap="hideModal">知道了</button>
				</view>
			</view>
		</view>
		<loadingBar :loadModal="loadModal"></loadingBar>
	</view>
</template>

<script>
	import loading from '@/pages/component/loading/loading.vue'
	export default {
		components: {
			loadingBar: loading
		},
		data() {
			return {
				modalName: '',
				content: '通过您的授权，小程序将从pmi.org网站中查询您的成绩。另外：数据仅作查询使用，小程序不会保存您的账号和密码信息，谢谢使用！',
				save: true,
				username: '',
				password: '',
				loadModal: false,
				total:0,
				error:'',
			}
		},
		computed:{
			num(){
				return 1238+this.total;
			}
		},
		methods: {
			onShareAppMessage: (res) => {
				return {
					title: 'PMP成绩查询',
					path: '/pages/index/index',
					success: (res) => {
						console.log("转发成功", res);
					},
					fail: (res) => {
						console.log("转发失败", res);
					}
				}
			},
			async getOpenid() {
				var openid = uni.getStorageSync('openid');
				if (openid) {
					return [null, openid]
				}

				var [error, loginRes] = await uni.login();
				
				if (error) {
					return [error]
				}

				var [_error, _res] = await this.$http({
					url: `/wx/openid/${loginRes.code}?platform=${this.$platform}`,
					method: 'get',
					setTimeout: 16 * 1000
				})
				
				if (_res && _res.statusCode === 200 && _res.data.ret === 0) {
					openid = _res.data.openid
					uni.setStorageSync('openid', openid)
					return [null, openid]
				}

				return [_error  || '网络错误，稍后再试！',null]
			},
			async getLoginInfo(openid,username,password) {
				var [error, res] = await this.$http({
					url: `/wx/login/${openid}?platform=${this.$platform}`,
					method: 'post',
					data: {
						'username': username,
						'password': password
					}
				})
				
				if (error || res.statusCode !== 200) {
					return [error  || '网络错误，稍后再试！', null]
				}

				if (res.data.ret !== 0) {
					return [res.data.msg, null]
				}

				return [null, res.data]
			},
			async loginHandler() {
				if(this.username.length === 0 || this.password.lenght === 0){
					this.modalName = 'error'
					this.error = '请正确填写账号和密码！'
					return
				}
				this.loadModal = true;
				var [error, openid] = await this.getOpenid()
				if (error) {
					this.loadModal = false;
					this.modalName = 'error'
					this.error = error
					return 
				}

				if (this.save) {
					uni.setStorageSync('username', this.username);
					uni.setStorageSync('password', this.password);
				} else {
					uni.removeStorageSync('username');
					uni.removeStorageSync('password');
				}

				var [error, data] = await this.getLoginInfo(openid, this.username, this.password)
				if (error) {
					this.loadModal = false;
					this.modalName = 'error'
					this.error = error || '网络错误，稍后再试！'
					return 
				}
				uni.setStorageSync('item', data.item)
				setTimeout(() => {
					this.loadModal = false;
					uni.navigateTo({
						url: `/pages/scores/scores`
					})
				}, 2000)
			},
			about() {
				this.modalName = 'Modal'
			},
			hideModal() {
				this.modalName = ''
			},
			async getNews(){
				var [error ,res] = await this.$http({
					url:'/wx/news'
				})
				
				if(error || res.statusCode!==200){
					return
				}
				
				if(res.data.ret !== 0){
					return
				}
								
				this.total = res.data.item.total
			}
		},		
		onLoad() {
			this.username = uni.getStorageSync('username') || '';
			this.password = uni.getStorageSync('password') || ''		
			this.getNews()
		}
	}
</script>

<style>
	.logo {
		width: 100%;
	}

	.ps {}
</style>
